/*
*	zzh 20170105
*/
#ifndef _z9_ORDER_H
#define _z9_ORDER_H

#include <stdint.h>
#include <arpa/inet.h>

#include <endian.h>

static inline uint16_t z9_swap16(uint16_t num) {
	return ((num<<8) & 0xFF00) | ((num & 0xFF00) >> 8);
}

static inline uint32_t z9_swap32(uint32_t num) {
	return  (z9_swap16(((uint16_t*)&num)[0]) << 16) | z9_swap16(((uint16_t*)&num)[1]); 
}

static inline uint64_t z9_swap64(uint64_t num) {
	return ((uint64_t)z9_swap32(((uint32_t*)&num)[0]) << 32) | z9_swap32(((uint32_t*)&num)[1]);
}

#define z9_hton16(x) ((uint16_t)htons(x))

#define z9_ntoh16(x) ((uint16_t)ntohs(x))

#define z9_hton32(x) ((uint32_t)htonl(x))

#define z9_ntoh32(x) ((uint32_t)ntohl(x))

static inline uint64_t z9_hton64(uint64_t num) {
#if __BYTE_ORDER == __BIG_ENDIAN 
	return num;
#else
	return z9_swap64(num);
#endif
}

static inline uint64_t z9_ntoh64(uint64_t num) {
#if __BYTE_ORDER == __BIG_ENDIAN 
	return num;
#else
	return z9_swap64(num);
#endif
}


#endif